﻿#include "cdbtableinfomgr.h"
#include "dbstruct.h"
#include <QMutex>
#include <QDebug>
#include <QSqlRecord>

CDbTableInfoMgr::CDbTableInfoMgr()
    : m_sMode("")
    , m_sTableName("")
    , m_pMutex(new QMutex())
    , m_bInitRecord(false)
    , m_bSqlOperIsOk(false)
    , m_nWorkType(enSQLUNKNOW)
    , m_nLimit(-1)
    , m_sSql("")
{
    m_mapFieldValue.clear();
    m_vecReRecord.clear();
    m_vecFilterFieldName.clear();
    m_vecFilterFieldValue.clear();
    m_vecResultFieldNameVar.clear();
    m_vecResultFieldValueVar.clear();
    m_vecOrderName.clear();
    m_vecOrderValue.clear();
    m_mapSymbol.clear();
    m_vecBatchData.clear();
    m_listErr.clear();
}

CDbTableInfoMgr::CDbTableInfoMgr(const CDbTableInfo &info)
    : m_sMode(info.sMode)
    , m_sTableName(info.sTableName)
    , m_pMutex(new QMutex())
    , m_bInitRecord(false)
    , m_bSqlOperIsOk(false)
    , m_nWorkType(enSQLUNKNOW)
    , m_nLimit(-1)
    , m_sSql("")
{
    m_mapFieldValue.clear();
    m_vecReRecord.clear();
    m_vecFilterFieldName.clear();
    m_vecFilterFieldValue.clear();
    m_vecResultFieldNameVar.clear();
    m_vecResultFieldValueVar.clear();
    m_vecOrderName.clear();
    m_vecOrderValue.clear();
    m_mapSymbol.clear();
    m_vecBatchData.clear();
    m_listErr.clear();
}

CDbTableInfoMgr::~CDbTableInfoMgr()
{
    if(nullptr != m_pMutex)
    {
        //m_pMutex->lock();
        delete m_pMutex;
        m_pMutex = nullptr;
    }
}

void CDbTableInfoMgr::setWorkType(const int &nWorkType)
{
    m_nWorkType = nWorkType;
}

void CDbTableInfoMgr::setTransactionID(const QString &sTransactionID)
{
    m_sTransactionID = sTransactionID;
}

void CDbTableInfoMgr::addFilterName(const QString &sFilter)
{
    QStringList listStr = sFilter.split(";");
    listStr.removeAll("");
    if(listStr.isEmpty()) return;
    foreach (QString sSrc, listStr)
    {
        m_vecFilterFieldName.append(sSrc);
    }
}

void CDbTableInfoMgr::addFilterName(const QVector<QString> &vecFilter)
{
    m_vecFilterFieldName.append(vecFilter);
}

void CDbTableInfoMgr::addFilterValue(const QString &sValue)
{
    m_vecFilterFieldValue.append(sValue);
}

void CDbTableInfoMgr::addFilterValue(const QVector<QString> &vecValue)
{
    m_vecFilterFieldValue.append(vecValue);
}

void CDbTableInfoMgr::addResultField(const QString &sField)
{
    QStringList listStr = sField.split(";");
    listStr.removeAll("");
    if(listStr.isEmpty()) return;
    foreach (QString sSrc, listStr)
    {
        m_vecResultFieldNameVar.append(sSrc);
    }
}

void CDbTableInfoMgr::addResultField(const QVector<QString> &vecField)
{
    foreach (QString str, vecField)
    {
        m_vecResultFieldNameVar.append(str);
    }
}

void CDbTableInfoMgr::addResultField(const QVector<QVariant> &vecField)
{
    m_vecResultFieldNameVar.append(vecField);
}

void CDbTableInfoMgr::addResultValue(const QString &sValue)
{
    m_vecResultFieldValueVar.append(sValue);
}

void CDbTableInfoMgr::addResultValue(const QVector<QString> &vecValue)
{
    foreach (QString str, vecValue)
    {
        m_vecResultFieldValueVar.append(str);
    }
}

void CDbTableInfoMgr::addResultValue(const QVector<QVariant> &vecValue)
{
    m_vecResultFieldValueVar.append(vecValue);
}

void CDbTableInfoMgr::addOrderByField(const QString &sField)
{
    QStringList listStr = sField.split(";");
    listStr.removeAll("");
    if(listStr.isEmpty()) return;
    foreach (QString sSrc, listStr)
    {
        m_vecOrderName.append(sSrc);
    }
}

void CDbTableInfoMgr::addOrderByField(const QVector<QString> &vecField)
{
    m_vecOrderName.append(vecField);
}

void CDbTableInfoMgr::addOrderByRule(const QString &sRule)
{
    QStringList listStr = sRule.split(";");
    listStr.removeAll("");
    if(listStr.isEmpty()) return;
    foreach (QString sSrc, listStr)
    {
        m_vecOrderValue.append(sSrc);
    }
}

void CDbTableInfoMgr::addOrderByRule(const QVector<QString> &vecRule)
{
    m_vecOrderValue.append(vecRule);
}

void CDbTableInfoMgr::addSymbol(const QMap<int, QString> &mapSymbol)
{
    foreach (int key, mapSymbol.keys())
    {
        m_mapSymbol.insert(key, mapSymbol.value(key));
    }
}

void CDbTableInfoMgr::addSymbol(const int &nIndex, const QString &symbol)
{
    m_mapSymbol.insert(nIndex, symbol);
}

void CDbTableInfoMgr::addLimit(const int &nLimit)
{
    m_nLimit = nLimit;
}

void CDbTableInfoMgr::addSql(const QString &sSql)
{
    m_sSql = sSql;
}

void CDbTableInfoMgr::addSql(const QString &sSql, const QMap<QString, QVariant> &mapBindValue)
{
    m_sSql= sSql;
    m_mapBindValue = mapBindValue;
}

void CDbTableInfoMgr::addBindValue(const QMap<QString, QVariant> &mapBindValue)
{
    m_mapBindValue = mapBindValue;
}

QVector<QSqlRecord> CDbTableInfoMgr::getReRecord()
{
    return m_vecReRecord;
}

bool CDbTableInfoMgr::tryLock(int nTime)
{
    if(nullptr == m_pMutex) return false;
    return m_pMutex->tryLock(nTime);
}

void CDbTableInfoMgr::unlock()
{
    if(nullptr == m_pMutex) return;
    tryLock(10);
    m_pMutex->unlock();
}

QString CDbTableInfoMgr::getLastSql()
{
    if(m_listErr.isEmpty()) return "";
    return m_listErr.at(0);
}

QString CDbTableInfoMgr::getLastError()
{
    if(2 > m_listErr.size()) return "";
    return m_listErr.at(1);
}

int CDbTableInfoMgr::getSqlType()
{
    return m_nSqlType;
}

void CDbTableInfoMgr::resetInfo()
{
    m_bSqlOperIsOk = false;
    m_nWorkType = enSQLUNKNOW;
    m_nLimit = -1;
    m_nSqlType = enSQLUNKNOW;
    m_sSql.clear();

    //shrink_to_fit函数Qt5.10才引入，清理可以使用squeeze或swap，感觉没有必要，暂不使用
    m_vecReRecord.clear();
    m_vecFilterFieldName.clear();
    m_vecFilterFieldValue.clear();
    m_vecResultFieldNameVar.clear();
    m_vecResultFieldValueVar.clear();
    m_vecOrderName.clear();
    m_vecOrderValue.clear();
    m_mapSymbol.clear();
    m_vecBatchData.clear();
    m_mapBindValue.clear();
    m_listErr.clear();
}

